package com.blood.modules.pregnancy.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blood.core.config.mybatisplus.ICommonServiceImpl;
import com.blood.core.enums.MessageStatusEnum;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.PageResult;
import com.blood.core.resp.ResponseResult;
import com.blood.core.util.bean.ListUtils;
import com.blood.core.util.http.RequestTheadUtils;
import com.blood.modules.pregnancy.dto.req.PregnancyMessagePageReq;
import com.blood.modules.pregnancy.dto.resp.PregnancyMessagePageResp;
import com.blood.modules.pregnancy.mapper.PregnancyMessageMapper;
import com.blood.modules.pregnancy.model.PregnancyMessage;
import com.blood.modules.pregnancy.service.IPregnancyMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


/**
 * 孕产通知Service业务层处理
 *
 * @author daoren
 * @date 2022-11-25 09:11:14
 */
@Service
public class PregnancyMessageServiceImpl extends ICommonServiceImpl<PregnancyMessageMapper, PregnancyMessage> implements IPregnancyMessageService {
    @Autowired
    private Snowflake snowFlake;

    @Override
    public PageResult<PregnancyMessagePageResp> pageList(PregnancyMessagePageReq req) {
        PageResult<PregnancyMessagePageResp> pageResult = PageResult.create(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<PregnancyMessage> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getPregnancyName())) {
            qw.like(PregnancyMessage::getPregnancyName, req.getPregnancyName());
        }
        if (StringUtils.isNotBlank(req.getTitle())) {
            qw.like(PregnancyMessage::getTitle, req.getTitle());
        }
        if (null!=req.getRedStatus()) {
            qw.eq(PregnancyMessage::getRedStatus, req.getRedStatus());
        }
        if (null != req.getType()) {
            qw.eq(PregnancyMessage::getType, req.getType());
            if (MessageStatusEnum.DIS_ENABLE.getValue().equals(req.getType())) {
                qw.eq(PregnancyMessage::getDisStatus, MessageStatusEnum.ENABLE.getValue());
            }
        }
        qw.eq(PregnancyMessage::getReceiveId, RequestTheadUtils.getUserId());
        qw.eq(PregnancyMessage::isDeleteFlag, false);
        qw.orderByDesc(PregnancyMessage::getCreateTime);

        IPage<PregnancyMessage> iPage = baseMapper.selectPage(new Page<>(req.getPageNo(), req.getPageSize()), qw);
        if (iPage.getRecords().size() > 0) {
            List<PregnancyMessagePageResp> queryRespList = ListUtils.copyProperties(iPage.getRecords(), PregnancyMessagePageResp.class);
            return pageResult.data(iPage.getTotal(), queryRespList);
        }
        return pageResult.data(0, Collections.emptyList());
    }

    @Override
    public PregnancyMessage queryNewInfo() {
        return baseMapper.selectOne(new QueryWrapper<PregnancyMessage>().lambda()
                .eq(PregnancyMessage::getRedStatus, MessageStatusEnum.ENABLE.getValue())
                .eq(PregnancyMessage::getReceiveId, RequestTheadUtils.getUserId())
                .eq(PregnancyMessage::isDeleteFlag, false)
                .orderByDesc(PregnancyMessage::getLineNo)
                .last("limit 1"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInfo(PregnancyMessage saveInfo) {
        String id = snowFlake.nextIdStr();
        saveInfo.setId(id);
        baseMapper.insert(saveInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateInfo(PregnancyMessage req) {
        PregnancyMessage updateInfo = new PregnancyMessage();
        updateInfo.setDisStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        updateInfo.setUpdateBy(RequestTheadUtils.getUserId());
        updateInfo.setRedStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        baseMapper.update(updateInfo, new UpdateWrapper<PregnancyMessage>().lambda()
                .eq(PregnancyMessage::getGatherId, req.getGatherId())
                .eq(PregnancyMessage::getType, MessageStatusEnum.DIS_ENABLE.getValue())
                .eq(PregnancyMessage::isDeleteFlag, false));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeInfo(String gatherId) {
        baseMapper.delete(new UpdateWrapper<PregnancyMessage>().lambda()
                .eq(PregnancyMessage::getGatherId,gatherId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateInfo(String pregnancyId) {
        PregnancyMessage updateInfo = new PregnancyMessage();
        updateInfo.setDisStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        updateInfo.setUpdateBy(RequestTheadUtils.getUserId());
        updateInfo.setRedStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        baseMapper.update(updateInfo, new UpdateWrapper<PregnancyMessage>().lambda()
                .eq(PregnancyMessage::getPregnancyId, pregnancyId)
                .eq(PregnancyMessage::getType, MessageStatusEnum.DIS_ENABLE.getValue())
                .eq(PregnancyMessage::isDeleteFlag, false));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> clear() {
        PregnancyMessage message = new PregnancyMessage();
        message.setDeleteFlag(true);
        message.setUpdateBy(RequestTheadUtils.getUserId());
        update(message, new UpdateWrapper<PregnancyMessage>()
                .lambda()
                .eq(PregnancyMessage::getType, MessageStatusEnum.ENABLE.getValue())
                .eq(PregnancyMessage::getReceiveId, RequestTheadUtils.getUserId()));
        return GlobalResultStatus.SUCCESS.toResult().responseEmptyBody();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void read(String id) {
        PregnancyMessage message = new PregnancyMessage();
        message.setRedStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        message.setUpdateBy(RequestTheadUtils.getUserId());
        updateById(message, id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allRead() {
        PregnancyMessage message = new PregnancyMessage();
        message.setRedStatus(MessageStatusEnum.DIS_ENABLE.getValue());
        message.setUpdateBy(RequestTheadUtils.getUserId());
        update(message, new UpdateWrapper<PregnancyMessage>()
                .lambda()
                .eq(PregnancyMessage::getReceiveId, RequestTheadUtils.getUserId()));
    }
}
